package action.account;

import java.util.List;

import model.Account;
import model.Message;
import model.Page;

import DAO.AccountDAO;
import DAO.DAOImple.AccountImple;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AccountAction extends ActionSupport {
	
	private Account account;
	private Message message;
	private Page page;
	List<Account> accounts;
	List<Message> messages;
	
	public String execute() {
		return SUCCESS;
	}
	
	//获得账目列表
	public String getAccountList(){
		AccountDAO accountDao = new AccountImple();
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Account where state = 0",page.getCurrentPage(),50);  //获得分页对象
		accounts = accountDao.getAccountList(page);
		
		return SUCCESS;
	}
	
	//增加账目
	public String addAccount() {
		String result = ERROR;
		
		ActionContext act = ActionContext.getContext();
		AccountDAO accountDao = new AccountImple();
		int addResult = accountDao.addAccount(account);
		if(addResult == 0) {
			act.put("addwrong", "0");  //添加失败
		}else {
			//添加成功
			if(page == null) {
				page = new Page();
				page.setCurrentPage(1);
			}
			page = page.getPage("T_Account where state = 0",page.getCurrentPage(),50);  //获得分页对象
			accounts = accountDao.getAccountList(page);  //获得账目列表
			result = SUCCESS;
		}
		return result;
	}

	//通过id获得account
	public String getAccountById() {
		
		AccountDAO accountDao = new AccountImple();
		account = accountDao.getAccount(account.getId());
		
		return SUCCESS;
	}
	
	//修改账目
	public String accountModify() {
		String result = ERROR;
		
		ActionContext act = ActionContext.getContext();
		AccountDAO accountDao = new AccountImple();
		int addResult = accountDao.modifyAccount(account);
		if(addResult == 0) {
			act.put("addwrong", "0");  //添加失败
		}else {
			//添加成功
			if(page == null) {
				page = new Page();
				page.setCurrentPage(1);
			}
			page = page.getPage("T_Account where state = 0",page.getCurrentPage(),50);  //获得分页对象
			accounts = accountDao.getAccountList(page);
			result = SUCCESS;
		}
		return result;
	}
	
	//删除账目
	public String deleteAccount() {
		String result = ERROR;
		
		ActionContext act = ActionContext.getContext();
		AccountDAO accountDao = new AccountImple();
		int addResult = accountDao.deleteAccount(account.getId());
		if(addResult == 0) {
			act.put("addwrong", "0");  //添加失败
		}else {
			//添加成功
			if(page == null) {
				page = new Page();
				page.setCurrentPage(1);
			}
			page = page.getPage("T_Account where state = 0",page.getCurrentPage(),50);  //获得分页对象
			accounts = accountDao.getAccountList(page);
			result = SUCCESS;
		}
		return result;
	}
	
	//账目留言板 取得留言内容
	public String accountMessage() {
		String result = ERROR;
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Message where messageType = 1 and state = 0",page.getCurrentPage(),20);  //获得分页对象
		AccountDAO accountDao = new AccountImple();
		
		messages = accountDao.getAccountMessages(page);
		
		return SUCCESS;
	}
	
	//账目留言板 增加留言
	public String addAccountMessage() {
		
		AccountDAO accountDao = new AccountImple();
		message.setMessageType(1); //设置留言类型为账目留言 1
		int addResult = accountDao.addAccountMessage(message);
		if(addResult == 0) {
			//System.out.println("失败");
		}else {
			//System.out.println("成功");
		}
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Message where messageType = 1 and state = 0",page.getCurrentPage(),20);  //获得分页对象
		messages = accountDao.getAccountMessages(page);
		return SUCCESS;
	}
	
	//删除账目板留言
	public String deleteMessage() {
		AccountDAO accountDao = new AccountImple();
		message.setMessageType(1); //设置留言类型为账目留言 1
		int addResult = accountDao.deleteMessage(message.getId());
		if(addResult == 0) {
			//System.out.println("失败");
		}else {
			//System.out.println("成功");
		}
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Message where messageType = 1 and state = 0",page.getCurrentPage(),20);  //获得分页对象
		messages = accountDao.getAccountMessages(page);
		return SUCCESS;
	}
	
	public void setAccount(Account account) {
		this.account = account;
	}

	public List<Account> getAccounts() {
		return accounts;
	}

	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}

	public Account getAccount() {
		return account;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public List<Message> getMessages() {
		return messages;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
}
